package com.sekill.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sekill.goods.entity.Stock;
import com.sekill.goods.entity.TbSku;
import org.springframework.ui.Model;
import com.sekill.goods.entity.Seckill;
import com.sekill.goods.service.ISeckillService;
import com.sekill.goods.service.IStockService;
import com.sekill.goods.service.ITbSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huhu
 * @since 2021-06-21
 */
@CrossOrigin
@RestController
@RequestMapping("/seckill")
public class SeckillController {

    @Autowired
    private ISeckillService iSeckillService;

    @Autowired
    private ITbSkuService iTbSkuService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IStockService iStockService;

    @GetMapping("getgoods")
    public List<Seckill> getgoods(Model model) throws Exception {
                //iSeckillService.ftlToHtml();
        List<Seckill> getlist = iSeckillService.getlist();

        return getlist;
    }

    @GetMapping("getByid")
    public Seckill getByid(Integer skuId) throws Exception {
       // iSeckillService.ftlToHtml(skuId);
        Seckill goods = iSeckillService.getbyids(skuId);

        return goods;
    }

    //根据状态查询秒杀列表
    @GetMapping("getListStatus")
    public List<Seckill> getListStatus(@RequestParam("status") Integer status) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",status);
        List<Seckill> list = iSeckillService.list(queryWrapper);
        for (int i = 0; i < list.size(); i++) {
            Seckill seckill = list.get(i);
            QueryWrapper qw = new QueryWrapper<>();
            qw.eq("sku_id",seckill.getSkuId());
            Stock one = iStockService.getOne(qw);
            seckill.setSeckillStock(one.getSeckillStock());
            TbSku byId = iTbSkuService.getById(seckill.getSkuId());
            seckill.setTitle(byId.getTitle());
            seckill.setImages(byId.getImages());
        }
        return list;
    }

    //修改秒杀商品状态
    @GetMapping("updateSeckillStatus")
    public boolean updateSeckillStatus(@RequestParam("status") Integer status,@RequestParam("skuId") Long skuId){
        boolean result = iSeckillService.updateSeckillStatus(status,skuId);
        return result;
    }

}
